package com.wc._16届国特冲刺营._09_数据结构_线段树屠龙术.封印宝石;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2025/5/31 17:03
 * @description
 * https://www.lanqiao.cn/courses/51805/learning/?id=4072922&compatibility=false
 */
public class Main {
    /**
     * 思路：
     * 线段树 寻找最大值与次大值
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 100010, M = N << 2;
    static int[] maxv1 = new int[M], maxv2 = new int[M], i1 = new int[M], i2 = new int[M];
    static int[] a = new int[N], res = new int[N];
    static int n, m;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 1; i <= n; i++) a[i] = sc.nextInt();
        build(1, 1, n);
        for (int i = 1; i <= n; i++) {
            int[] t = query(1, 1, n, i, Math.min(n, i + m), res[i - 1] == -1 ? 0 : res[i - 1]);
            res[i] = t[0];
            if (t[0] == -1) continue;
            m -= t[1] - i;
            update(1, 1, n, t[1]);
        }
        for (int i = 1; i <= n; i++) out.print(res[i] + " ");
        out.println();
        out.flush();
    }

    static void build(int u, int l, int r) {
        if (l == r) {
            maxv1[u] = a[l];
            i1[u] = l;
            maxv2[u] = -1;
            i2[u] = 0;
            return;
        }
        int mid = l + r >> 1;
        build(u << 1, l, mid);
        build(u << 1 | 1, mid + 1, r);
        pushUp(u);
    }

    static void pushUp(int u) {
        int l = u << 1, r = u << 1 | 1;
        if (maxv1[l] == maxv1[r]) {
            maxv1[u] = maxv1[l];
            i1[u] = i1[l];
            if (maxv2[l] >= maxv2[r]) {
                maxv2[u] = maxv2[l];
                i2[u] = i2[l];
            } else {
                maxv2[u] = maxv2[r];
                i2[u] = i2[r];
            }
        } else if (maxv1[l] > maxv1[r]) {
                maxv1[u] = maxv1[l];
                i1[u] = i1[l];
                if (maxv2[l] >= maxv1[r]) {
                    maxv2[u] = maxv2[l];
                    i2[u] = i2[l];
                } else {
                    maxv2[u] = maxv1[r];
                    i2[u] = i1[r];
                }
        } else {
            maxv1[u] = maxv1[r];
            i1[u] = i1[r];
            if (maxv2[r] >= maxv1[l]) {
                maxv2[u] = maxv2[r];
                i2[u] = i2[r];
            } else {
                maxv2[u] = maxv1[l];
                i2[u] = i1[l];
            }
        }
    }

    static int[] query(int u, int l, int r, int ql, int qr, int x) {
        if (l == ql && r == qr) {
            // 这里限制了不可能等于 x 了
            if (maxv1[u] == x) return new int[]{maxv2[u], i2[u]};
            return new int[]{maxv1[u], i1[u]};
        }

        int mid = l + r >> 1;
        if (qr <= mid) return query(u << 1, l, mid, ql, qr, x);
        if (ql > mid) return query(u << 1 | 1, mid + 1, r, ql, qr, x);
        int[] L = query(u << 1, l, mid, ql, mid, x);
        int[] R = query(u << 1 | 1, mid + 1, r, mid + 1, qr, x);
        if (L[0] >= R[0]) return L;
        return R;
    }

    static void update(int u, int l, int r, int x) {
        if (l == r) {
            maxv1[u] = maxv2[u] = -1;
            i1[u] = i2[u] = 0;
            return;
        }
        int mid = l + r >> 1;
        if (x <= mid) update(u << 1, l, mid, x);
        else update(u << 1 | 1, mid + 1, r, x);
        pushUp(u);
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

